#ifndef	WF_STARTSTOP_GLOBALS
#define	WF_STARTSTOP_GLOBALS
#include <includes.h>
#include "WF_StartStop.h"

static uint8_t ShutDownAlarm_WorkStepHandle(uint8_t *TempWFStep,uint8_t *TempWFPeriod,uint8_t *WFStaChange,uint8_t Solution)
{
  if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))//检查控制器的运行状态是否在停机告警态
  {
  	if(Solution&GenFaultWay_Action)
    {
        SetNewWorkFlowStepStatus(TempWFStep,TempWFPeriod,WFStaChange,StartStopStep_endStep);
        return(DEF_TRUE);
    }
  }
  
  return(DEF_FALSE);
}

uint8_t StartStopStep_1st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    CurrProcCountdownEnable = DEF_TRUE;
    SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,StartStopStep_Maximum);
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 合闸前检查
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t StartStopStep_2st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    ActInform |= SwitchBefCheckStep(TempWorkflow,TempWFStep,TempWFPeriod,StartStopStep_Maximum,
        share_parameterCfgData_st.startStopCfg_st.EngineInitStop_time,share_parameterCfgData_st.startStopCfg_st.EngineInitSpeed,ShutDownAlarm_WorkStepHandle);
    return (ActInform);
}
/**
 * @brief 开始采集并启动
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t StartStopStep_3st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    /* AlarmShutDown     */
    if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
    {
        UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
        return (ActInform);
    }
    switch (TempWFPeriod)
    {
    case WorkflowPeriod_Entry:
        // 波形
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_Waveform,BitOperaOpt_Set);
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_StartStop,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        break;
    case WorkflowPeriod_1st:
        if(PidReqPriMoverStartStop(share_parameterCfgData_st.startStopCfg_st.EngineStartSpeed,share_parameterCfgData_st.startStopCfg_st.EngineStart_time,
            PriMover_ENUM_Type_Start,share_parameterCfgData_st.startStopCfg_st.EngineStart_time+5,ShareInOrderSta_PriMoverStart))
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
        }
        break;
    case WorkflowPeriod_2nd:
        if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_PriMoverStart])==PIDOrderSta_DEF_SUCCESS)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,StartStopStep_Maximum);
        }
        break;
    }

    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

/**
 * @brief 处理采集数据
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t StartStopStep_4st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    /* AlarmShutDown     */
    if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
    {
        UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
        return (ActInform);
    }
    switch (TempWFPeriod)
    {
        case WorkflowPeriod_Entry:
            ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_StartStopEnd,BitOperaOpt_Set);
            ActInform |= WorkflowOutRequestBit_WaveRequest;
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
            break;
        case WorkflowPeriod_1st:
            share_StartStopData_st.baseData_st.MaxExciGenVolt_start=TemMax_GenPhaseVoltAverage;
            share_StartStopData_st.baseData_st.MaxExciGenFreq_start=TemMax_GenFreqAverage;
            share_StartStopData_st.baseData_st.MaxExciVoltage_start=TemMax_ExcitationVolt;
            share_StartStopData_st.baseData_st.MaxExciCurrent_start=TemMax_ExcitationCurr;
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
            break;
        case WorkflowPeriod_2nd:
            // 采集稳态
            ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_SteadyVoltage,BitOperaOpt_Set);
            ActInform |= WorkflowOutRequestBit_WaveRequest;
            SMTimerCounter = share_parameterCfgData_st.startStopCfg_st.SteadyTest_time * WorkFlowTimingFreq;
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_3nd);
            break;
        case WorkflowPeriod_3nd:
            if(SMTimerCounter==0)
            {
                ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_SteadyVoltageEnd,BitOperaOpt_Set);
                ActInform |= WorkflowOutRequestBit_WaveRequest;
                SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_4nd);
            }
            break;
        case WorkflowPeriod_4nd:
            // 计算
            share_StartStopData_st.baseData_st.SteadyVoltage_U = TemSUM_GenPhaseVoltL1/TemCount;
            share_StartStopData_st.baseData_st.SteadyVoltage_V = TemSUM_GenPhaseVoltL2/TemCount;
            share_StartStopData_st.baseData_st.SteadyVoltage_W = TemSUM_GenPhaseVoltL3/TemCount;
            share_StartStopData_st.baseData_st.SteadyAverageVolt = TemSUM_GenPhaseVoltAverage/TemCount;
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,StartStopStep_Maximum);
            break;
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 停机过程数据采集&处理
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t StartStopStep_5st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    /* AlarmShutDown     */
    if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
    {
        UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
        return (ActInform);
    }
    switch (TempWFPeriod)
    {
        case WorkflowPeriod_Entry:
            if(PidReqPriMoverStartStop(share_parameterCfgData_st.startStopCfg_st.EngineStopSpeed,share_parameterCfgData_st.startStopCfg_st.EngineStop_time,
                PriMover_ENUM_Type_Stop,share_parameterCfgData_st.startStopCfg_st.EngineStop_time+5,ShareInOrderSta_PriMoverStop))
            {
                ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_StartStop,BitOperaOpt_Set);
                ActInform |= WorkflowOutRequestBit_WaveRequest;
                SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
            }
            break;
        case WorkflowPeriod_1st:
            if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_PriMoverStop])==PIDOrderSta_DEF_SUCCESS)
            {
                ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_StartStopEnd,BitOperaOpt_Set);
                ActInform |= WorkflowOutRequestBit_WaveRequest;
                SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
            }
            break;
        case WorkflowPeriod_2nd:
            share_StartStopData_st.baseData_st.MaxExciGenVolt_stop=TemMax_GenPhaseVoltAverage;
            share_StartStopData_st.baseData_st.MaxExciGenFreq_stop=TemMax_GenFreqAverage;
            share_StartStopData_st.baseData_st.MaxExciVoltage_stop=TemMax_ExcitationVolt;
            share_StartStopData_st.baseData_st.MaxExciCurrent_stop=TemMax_ExcitationCurr;
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,StartStopStep_endStep);
            break;
    }

    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
uint8_t StartStopStep_end(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    uint8_t WorkflowMode = TempWorkflow;

    switch (TempWFPeriod)
    {
        case WorkflowPeriod_Entry:
            // 等待一秒观察后续波形
            SMTimerCounter = 1 * WorkFlowTimingFreq;
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
            break;
        case WorkflowPeriod_1st:
            if(SMTimerCounter==0)
            {
                CurrProcCountdownEnable=DEF_FALSE;
                ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_WaveformEnd,BitOperaOpt_Set);
                ActInform |= WorkflowOutRequestBit_WaveRequest;
                SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
            }
            break;
        case WorkflowPeriod_2nd:
            FlowDataStatus |= (1<<WorkflowMode_StartStop);
            if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))
            {
                SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_StopPriMover);
            }
            else
            {
                //正常停机计算数据
                SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_Ending);
                WaveSetFlowDataStatus(WorkflowMode);
            }
            break;
    }

    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

typedef uint8_t (*WorkFlowStepType)(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm);
static const WorkFlowStepType StartStopStepTable[] = {
    (WorkFlowStepType)StartStopStep_1st        ,
    (WorkFlowStepType)StartStopStep_2st        ,
    (WorkFlowStepType)StartStopStep_3st        ,
    (WorkFlowStepType)StartStopStep_4st        ,
    (WorkFlowStepType)StartStopStep_5st        ,
    (WorkFlowStepType)StartStopStep_end        ,
};


uint8_t WorkFlowHandling_StartStop(uint8_t TempWorkflow, uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE, WFStaChange = DEF_FALSE;

	ActInform |= (*StartStopStepTable[TempWFStep])(TempWorkflow,TempWFStep,TempWFPeriod, TempOutAlarm);

    /************************************/
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
#endif